#include "mat_ui_combo.h"
#include "mat_define.h"
#include "mat_char_animation.h"
#include "mat_game.h"
#include "mat_sounds.h"
#include "mat_level_manager.h"

#include "graphics/graphics_util.h"

MATSpritePopupBase::MATSpritePopupBase() :	m_UIState(UI_STATE_NONE),
							m_ScaleExtra(1.0f),
							m_Duration(1.0f),											
							m_Delay(0)							
{
	
	
}



MATLevelCompletePopup::MATLevelCompletePopup() : MATSpritePopupBase()
{
	m_Scale = (float)GraphicsUtil::WIDTH / (float)TEXTURE_WIDTH;
	m_Sprite[STATE_COMPLETE].Create("level_complete");
	m_Sprite[STATE_FAIL].Create("level_fail");

	m_Sprite[STATE_COMPLETE].m_Size *= m_Scale;
	m_Sprite[STATE_FAIL].m_Size *= m_Scale;

	m_Sprite[STATE_COMPLETE].m_Pos = m_Sprite[STATE_FAIL].m_Pos = math::Vec2( 0.5f,0.45f);
	m_Size = m_Sprite[STATE_COMPLETE].m_Size;
	


}

MATUICombo::MATUICombo() : MATSpritePopupBase()
{
	m_Scale = (float)GraphicsUtil::WIDTH / (float)TEXTURE_WIDTH;
	char Buffer[256];		
	for(int i = 0; i < 3; i++)
	{
		sprintf(Buffer, "%icombo", i+2);
		m_SpriteCombo[i].Create(Buffer);
		m_SpriteCombo[i].m_Size *= m_Scale;
		m_Size = m_SpriteCombo[i].m_Size;
		m_SpriteCombo[i].m_Pos = MATGame::GetBoardPos(4,4);

		m_pSprite = &m_SpriteCombo[i];
	}
}

void			MATUICombo::Show(int Combo, float Delay, float ShowTime)
{
	int Index = Combo-2;
	if(Index >= 0 && Index < 3)
	{
		m_pSprite = &m_SpriteCombo[Combo-2];
		MATSpritePopupBase::Show(Delay,ShowTime);
	}
}

void	MATLevelCompletePopup::Show(STATE_LEVEL State, float Delay, float ShowTime)
{
	m_pSprite = &m_Sprite[State];
	//m_Size = m_Sprite[State].m_Size;
	MATSpritePopupBase::Show(Delay,ShowTime);

}

void	MATLevelCompletePopup::OnShow()
{
	m_pSprite = &m_Sprite[(MATLevelCompletePopup::STATE_LEVEL) MATLevelManager::Instance().IsLevelSuccess()];
}



void			MATSpritePopupBase::Show( float Delay, float ShowTime)
{
	
	MATCharAnimation::Instance().SetAnimation(MATCharAnimation::ANIMATION_SURPRISE);
	m_UIState = UI_STATE_START;
	

	m_Delay = Delay;
	m_Duration = ShowTime;
	
	
}

void			MATSpritePopupBase::Render()
{
	if(m_UIState == UI_STATE_NONE)
		return;

	if(m_Delay > 0)			
		return;
	

	m_pSprite->RenderRotated();
	

}
	
void			MATSpritePopupBase::Tick(float dt)
{
	if(m_Delay > 0)
	{
		m_Delay -= dt;
		if(m_Delay <= 0)
		{
			OnShow();
		}
		return;
	}

	switch(m_UIState)
	{
	case UI_STATE_NONE:
		break;
	case UI_STATE_START:
		m_ScaleExtra = 0.5f;
		m_pSprite->m_Size = m_Size * m_ScaleExtra;
		m_UIState = UI_STATE_BIGGER;		
		break;
	case UI_STATE_BIGGER:
		m_pSprite->m_Size = m_Size * m_ScaleExtra;
		m_ScaleExtra += ( dt * 8.0f);
		if(m_ScaleExtra >= 1.2f)
		{
			m_ScaleExtra = 1.2f;
			m_UIState = UI_STATE_RESMALLER;

		}
		break;

	case UI_STATE_RESMALLER:
		m_pSprite->m_Size = m_Size * m_ScaleExtra;
		m_ScaleExtra -= ( dt * 4.0f);
		if(m_ScaleExtra <= 1.0f)
		{
			m_ScaleExtra = 1.0f;
			m_UIState = UI_STATE_STILL;
			MATSounds::Instance().PlaySoundSFX(MATSounds::UI_POPUP);
		}
		break;
	case UI_STATE_STILL:
		m_Duration -= dt;
		if(m_Duration <= 0)
			m_UIState = UI_STATE_BIGGERAGAIN;
		break;
	case UI_STATE_BIGGERAGAIN:
		m_pSprite->m_Size = m_Size * m_ScaleExtra;
		m_ScaleExtra += ( dt * 8.0f);
		if(m_ScaleExtra >= 1.5f)
		{
			m_ScaleExtra = 1.5f;
			m_UIState = UI_STATE_NONE;
		}
		break;
	case UI_STATE_DISAPPEAR:
		m_UIState = UI_STATE_NONE;
		break;
	default:
		break;
	}
}

float	MATSpritePopupBase::GetShowingTime()const
{
	return m_Delay + m_Duration;
}

